import java.util.Arrays;

/**
 * @author wangj881
 * @date 2024-11-19 9:25
 */
public class L977_2 {
    public static void main(String[] args) {
        int[] nums = {-4, -1, 0, 3, 10};
        int[] ints = new Solution().sortedSquares(nums);
        System.out.println(Arrays.toString(ints));
    }


    static class Solution {
        public int[] sortedSquares(int[] nums) {
            int left = 0;
            int right = nums.length - 1;
            int[] newArr = new int[nums.length];
            int index = nums.length - 1;
            while (left <= right) {
                if (nums[right] * nums[right] >= nums[left] * nums[left]) {
                    newArr[index] = nums[right] * nums[right];
                    right--;
                } else {
                    newArr[index] = nums[left] * nums[left];
                    left++;
                }
                index--;
            }
            return newArr;
        }
    }
}
